﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using CNotebook.Sales;

namespace NotebookS2.Sales.Transactions
{
    public partial class SOApproval : Form
    {
        public SOApproval()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string ActiveUserID { get; set; }

        public DataTable PostedLineItems { get; set; }
        public DataTable ApprovedLineItems { get; set; }
        public DataTable DisapprovedLineItems { get; set; }
        public DataTable OnHoldLineItems { get; set; }


        void BindPosted()
        {
            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oConnection.ConnectionString = this.ConnectionString;
            oConnection.Open();
            oCommand.Connection = oConnection;
            oCommand.CommandText = "SELECT *, Cast(0 as bit) IsSelected FROM ORDR WHERE DOCSTATUS='Posted'";

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            oConnection.Close();

            this.PostedLineItems = dt;
            gridControl1.DataSource = this.PostedLineItems;
            gridControl1.Refresh();

        }


        void BindApproved()
        {
            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oConnection.ConnectionString = this.ConnectionString;
            oConnection.Open();
            oCommand.Connection = oConnection;
            oCommand.CommandText = "SELECT *, Cast(0 as bit) IsSelected FROM ORDR WHERE DOCSTATUS='Approved'";

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            oConnection.Close();

            this.ApprovedLineItems = dt;
            gridControl2.DataSource = this.ApprovedLineItems;
            gridControl2.Refresh();

        }


        void BindHold()
        {
            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oConnection.ConnectionString = this.ConnectionString;
            oConnection.Open();
            oCommand.Connection = oConnection;
            oCommand.CommandText = "SELECT *, Cast(0 as bit) IsSelected FROM ORDR WHERE DOCSTATUS='On-Hold'";

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            oConnection.Close();

            this.OnHoldLineItems = dt;
            gridControl4.DataSource = this.OnHoldLineItems;
            gridControl4.Refresh();

        }

        void BindDisapproved()
        {
            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oConnection.ConnectionString = this.ConnectionString;
            oConnection.Open();
            oCommand.Connection = oConnection;
            oCommand.CommandText = "SELECT *, Cast(0 as bit) IsSelected FROM ORDR WHERE DOCSTATUS='Disapproved'";

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            oConnection.Close();

            this.DisapprovedLineItems = dt;
            gridControl3.DataSource = this.DisapprovedLineItems;
            gridControl3.Refresh();

        }

        private void barExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void SOApproval_Load(object sender, EventArgs e)
        {
            BindPosted();
            BindApproved();
            BindDisapproved();
            BindHold();
        }

        private void barRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            BindPosted();
            BindApproved();
            BindDisapproved();
            BindHold();
        }

        private void barApproved_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (xtraTabControl1.SelectedTabPageIndex == 0)
            {
                if (MessageBox.Show("Are you sure you want to approved those Sales Order?", "Sales Order", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
                {
                    SqlConnection oConnection = new SqlConnection();
                    SqlCommand oCommand = new SqlCommand();
                    SqlDataAdapter oAdapter = new SqlDataAdapter();
                    DataSet ds = new DataSet();

                    oConnection.ConnectionString = this.ConnectionString;
                    oConnection.Open();
                    foreach (DataRow oRow in this.PostedLineItems.Rows)
                    {
                        if (Convert.ToBoolean(oRow["IsSelected"]) == true)
                        {
                            oCommand = new SqlCommand();
                            oCommand.Connection = oConnection;
                            oCommand.CommandText = "UPDATE ORDR SET DOCSTATUS = 'Approved' WHERE DocNum=@DocNum";
                            oCommand.Parameters.Add(new SqlParameter("@DocNum", oRow["DocNum"].ToString()));

                            oCommand.ExecuteNonQuery();

                            oCommand = new SqlCommand();
                            oAdapter = new SqlDataAdapter();

                            oCommand.Connection = oConnection;
                            oCommand.CommandText = "SELECT * FROM ORDR WHERE DocNum = @DocNum";

                            oAdapter.SelectCommand = oCommand;


                        }
                    }
                    oConnection.Close();
                    BindPosted();
                    BindDisapproved();
                    BindApproved();
                    BindHold();
                }
            }
            else if (xtraTabControl1.SelectedTabPageIndex == 3)
            {
                if (MessageBox.Show("Are you sure you want to approved those Sales Order?", "Sales Order", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
                {
                    SqlConnection oConnection = new SqlConnection();
                    SqlCommand oCommand = new SqlCommand();
                    SqlDataAdapter oAdapter = new SqlDataAdapter();
                    DataSet ds = new DataSet();

                    oConnection.ConnectionString = this.ConnectionString;
                    oConnection.Open();
                    foreach (DataRow oRow in this.OnHoldLineItems.Rows)
                    {
                        if (Convert.ToBoolean(oRow["IsSelected"]) == true)
                        {
                            oCommand = new SqlCommand();
                            oCommand.Connection = oConnection;
                            oCommand.CommandText = "UPDATE ORDR SET DOCSTATUS = 'Approved' WHERE DocNum=@DocNum";
                            oCommand.Parameters.Add(new SqlParameter("@DocNum", oRow["DocNum"].ToString()));

                            oCommand.ExecuteNonQuery();

                            oCommand = new SqlCommand();
                            oAdapter = new SqlDataAdapter();

                            oCommand.Connection = oConnection;
                            oCommand.CommandText = "SELECT * FROM ORDR WHERE DocNum = @DocNum";

                            oAdapter.SelectCommand = oCommand;


                        }
                    }
                    oConnection.Close();
                    BindPosted();
                    BindDisapproved();
                    BindApproved();
                    BindHold();
                }
            }

        }

        private void barDisapproved_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (xtraTabControl1.SelectedTabPageIndex == 0)
            {
                if (MessageBox.Show("Are you sure you want to disapproved those Sales Order?", "Sales Order", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
                {
                    SqlConnection oConnection = new SqlConnection();
                    SqlCommand oCommand = new SqlCommand();
                    SqlDataAdapter oAdapter = new SqlDataAdapter();
                    DataSet ds = new DataSet();

                    oConnection.ConnectionString = this.ConnectionString;
                    oConnection.Open();
                    foreach (DataRow oRow in this.PostedLineItems.Rows)
                    {
                        if (Convert.ToBoolean(oRow["IsSelected"]) == true)
                        {
                            oCommand = new SqlCommand();
                            oCommand.Connection = oConnection;
                            oCommand.CommandText = "UPDATE ORDR SET DOCSTATUS = 'Disapproved' WHERE DocNum=@DocNum";
                            oCommand.Parameters.Add(new SqlParameter("@DocNum", oRow["DocNum"].ToString()));

                            oCommand.ExecuteNonQuery();
                        }
                    }
                    oConnection.Close();
                    BindPosted();
                    BindDisapproved();
                    BindApproved();
                    BindHold();
                }
            }
            else if (xtraTabControl1.SelectedTabPageIndex == 3)
            {
                if (MessageBox.Show("Are you sure you want to disapproved those Sales Order?", "Sales Order", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
                {
                    SqlConnection oConnection = new SqlConnection();
                    SqlCommand oCommand = new SqlCommand();
                    SqlDataAdapter oAdapter = new SqlDataAdapter();
                    DataSet ds = new DataSet();

                    oConnection.ConnectionString = this.ConnectionString;
                    oConnection.Open();
                    foreach (DataRow oRow in this.OnHoldLineItems.Rows)
                    {
                        if (Convert.ToBoolean(oRow["IsSelected"]) == true)
                        {
                            oCommand = new SqlCommand();
                            oCommand.Connection = oConnection;
                            oCommand.CommandText = "UPDATE ORDR SET DOCSTATUS = 'Disapproved' WHERE DocNum=@DocNum";
                            oCommand.Parameters.Add(new SqlParameter("@DocNum", oRow["DocNum"].ToString()));

                            oCommand.ExecuteNonQuery();
                        }
                    }
                    oConnection.Close();
                    BindPosted();
                    BindDisapproved();
                    BindApproved();
                    BindHold();
                }
            }
        }

        private void barHold_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (xtraTabControl1.SelectedTabPageIndex == 0)
            {
                if (MessageBox.Show("Are you sure you want to approved those Sales Order?", "Sales Order", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
                {
                    SqlConnection oConnection = new SqlConnection();
                    SqlCommand oCommand = new SqlCommand();
                    SqlDataAdapter oAdapter = new SqlDataAdapter();
                    DataSet ds = new DataSet();

                    oConnection.ConnectionString = this.ConnectionString;
                    oConnection.Open();
                    foreach (DataRow oRow in this.PostedLineItems.Rows)
                    {
                        if (Convert.ToBoolean(oRow["IsSelected"]) == true)
                        {
                            oCommand = new SqlCommand();
                            oCommand.Connection = oConnection;
                            oCommand.CommandText = "UPDATE ORDR SET DOCSTATUS = 'On-Hold' WHERE DocNum=@DocNum";
                            oCommand.Parameters.Add(new SqlParameter("@DocNum", oRow["DocNum"].ToString()));

                            oCommand.ExecuteNonQuery();
                        }
                    }
                    oConnection.Close();
                    BindPosted();
                    BindDisapproved();
                    BindApproved();
                    BindHold();
                }
            }
            else
            {
                MessageBox.Show("Please select for-approval Tab.", "Hold Sales Order", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
    }
}
